﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReferralTracker.Data.Entities;
using System.Data.Entity;
using ReferralTracker.Data.Contexts;

namespace ReferralTracker.Data.Repositories {
    public class UserRepository : Repository<User> {

        public UserRepository(IContext context)
            : base(context) {

        }

        public User FindByKey(int id) {
            return (from u in _data
                    where u.Id == id
                    select u).SingleOrDefault();
        }

        public User FindByEmail(string email) {
            return (from u in _data
                    where u.EMail == email
                    select u).SingleOrDefault();
        }

        public User FindByReferralId(string referralId) {
            return (from u in _data
                    where u.UserDetail.ReferralId == referralId
                    select u).SingleOrDefault();
        }

        public User FindByCredentials(string email, string password) {
            return (from u in _data
                    where u.EMail == email && u.Password == password
                    select u).SingleOrDefault();
        }

        public List<string> GetAllReferralId() {
            return (from u in _data
                    select u.UserDetail.ReferralId).ToList();
        }

        public bool ReferralIdExists(string referralId) {
            var retVal = false;
            var query = (from u in _data
                         where u.UserDetail.ReferralId == referralId
                         select u.UserDetail.ReferralId).SingleOrDefault();

            if (!string.IsNullOrEmpty(query)) retVal = true;

            return retVal;
        }
    }
}
